//
//  YZLoginVM.m
//  VET
//
//  Created by Lam BG on 2018/6/29.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZLoginVM.h"

#import "YZSignAPI.h"

#import "YZUser.h"

#import "MJExtension.h"
#import "OpenShareHeader.h"

#import "NSString+Predicate.h"

@implementation YZLoginVM

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.isHideWarning = YES;
//        [self setup];
    }
    
    return self;
}

- (void)setup {
    
    @weakify(self)
    [[RACObserve(self, number) distinctUntilChanged] subscribeNext:^(NSString *number) {
        
        @strongify(self)
        if ([number isValidMobileNumber]) {
            
            // 发起验证号码请求
            [SVProgressHUD show];
            [[YZSignAPI apiCheckUserMobile:number] subscribeNext:^(id  _Nullable x) {
                
                [SVProgressHUD dismiss];
                BOOL value = [x boolValue];
                if (value) {
                    
                    self.numberWarningInfo = @"";
                    self.isHideWarning = YES;
                } else {
                    
                    self.numberWarningInfo = [NSString stringWithFormat:@"帐号不存在[%@]", number];
                    self.isHideWarning = NO;
                }
                
            } error:^(NSError * _Nullable error) {
                
                [SVProgressHUD dismiss];
                self.numberWarningInfo = @"网络出错，请稍后再试";
                self.isHideWarning = NO;
            } completed:^{
                
                NSLog(@"completed");
            }];
        } else {
            
            // 当输入到达 11 位后 不是号码
            if (number.length == 11) {
                
                self.numberWarningInfo = @"请输入正确的手机号码";
                self.isHideWarning = NO;
            } else {
                
                self.numberWarningInfo = @"";
            }
        }
    }];
    
    
    // 登录
    _loginEnableSignal = [RACSignal combineLatest:@[RACObserve(self, number), RACObserve(self, password), RACObserve(self, isHideWarning)] reduce:^id(NSString *number, NSString *password, id isHideWarning){
        return @(1);
//        return @(number.length && password.length && [isHideWarning boolValue]);
    }];
    
    _loginCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
//            [[YZSignAPI apiLoginUserMobile:self.number andPassword:self.password deviceToken:[YZUser sharedInstance].deviceToken] subscribeNext:^(NSDictionary *data) {
//
//                // 保存用户数据
//                NSLog(@"%@", data);
//                [[YZUser sharedInstance] storeUserToLocal:data];
//                [subscriber sendNext:data];
//
//            } error:^(NSError * _Nullable error) {
//
//                NSLog(@"NSError: %@", error);
//                [subscriber sendError:error];
//
//            } completed:^{
//
//                NSLog(@"completed");
//                [subscriber sendCompleted];
//            }];
            
            return [RACDisposable disposableWithBlock:^{}];
        }];
    }];
    
    
    // 微信
    _weChatCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            // 微信登录
            [OpenShare WeixinAuth:@"snsapi_userinfo" Success:^(NSDictionary *data) {
                
                // 授权成功后获取 code = 081O4pLe1dtZXr0DW6Me1qmxLe1O4pLf;
                NSLog(@"微信登录成功:\n%@",data);
                if (data && data[@"code"]) {
                    
                    // 登录后台
                    NSString *code = data[@"code"];
                    [[YZSignAPI apiWeChatLogin:code] subscribeNext:^(NSDictionary *data) {
                        
                        // 保存用户数据
                        NSLog(@"%@", data);
                        [subscriber sendNext:data];
                        
                    } error:^(NSError * _Nullable error) {
                        
                        NSLog(@"NSError: %@", error);
                        [subscriber sendError:error];
                        
                    } completed:^{
                        
                        NSLog(@"completed");
                        [subscriber sendCompleted];
                    }];
                }
                
            } Fail:^(NSDictionary *message, NSError *error) {
                
                NSLog(@"微信登录失败:\n%@\n%@",message,error);
                [subscriber sendError:error];
            }];
            
            return [RACDisposable disposableWithBlock:^{}];
        }];
    }];
}

@end
